package cn.itsource.hrm.config;

import cn.itsource.hrm.constants.MQConstants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//mq的配置
@Configuration
public class RabbitMQConfig {

    //读取routingkey
    @Value("${site.routingkey}")
    private String routingKey;

    //交换机
    @Bean(MQConstants.NAME_EXCHANGE_PAGE_DIRECT)
    public Exchange exchange() {
        //durable(true)持久化，消息队列重启后交换机仍然存在 ：在mq中交换机的名字
        return ExchangeBuilder.topicExchange(MQConstants.NAME_EXCHANGE_PAGE_DIRECT).durable(true).build();
    }

    //声明队列
    @Bean(MQConstants.NAME_QUEUE_PAGE_DIRECT)
    public Queue queue() {
        Queue queue = new Queue(MQConstants.NAME_QUEUE_PAGE_DIRECT,true);
        return queue;
    }

    @Bean
    public Binding bindingQueueInformEmail() {
        return BindingBuilder.bind(queue()).to(exchange()).with(routingKey).noargs();
    }

}
